﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BMSQ.Admin
{
    public partial class Admin : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Common.UserInfo != null)
            {
                if (!IsPostBack)
                {
                    lblUser.Text = string.Format("欢迎您，{0}", Common.UserInfo.User.UserName);
                }
                LoadMenu();
                LoadSubMenu();
            }
            else
            {
                //Response.Redirect(Common.VirtualPath() + "/relogin.aspx");
                Response.Redirect("/relogin.aspx");
            }
        }

        private void LoadMenu()
        {
            divTopMenu.Controls.Clear();
            List<Vincent.Permission.Model.account_nodeslist> lst = Common.UserInfo.Nodes;

            lst = lst.Where(p => p.ParentID == "").OrderBy(p => p.OrderID).ToList();
            int index = 0;
            foreach (Vincent.Permission.Model.account_nodeslist node in lst)
            {
                LinkButton lb = new LinkButton();
                lb.ID = "lb" + index.ToString();
                lb.Text = node.Text;
                lb.PostBackUrl = node.Url;
                lb.Attributes.Add("NodeID", node.NodeID);
                lb.Click += new EventHandler(TopMenu_Click);
                divTopMenu.Controls.Add(lb);

                index++;
            }
        }

        private void LoadSubMenu()
        {
            string nodeID = Common.SelectedNodeID;

            if (!string.IsNullOrEmpty(nodeID))
            {
                Vincent.Permission.Model.account_nodeslist model = Common.UserInfo.Nodes.Where(p => p.NodeID == nodeID).FirstOrDefault();

                if (model != null)
                {
                    List<Vincent.Permission.Model.account_nodeslist> lst = Common.UserInfo.Nodes;

                    lst = lst.Where(p => p.ParentID == nodeID).OrderBy(p => p.OrderID).ToList();
                    divLeftMenu.Controls.Clear();

                    StringBuilder sbHtml = new StringBuilder();
                    sbHtml.Append("<span class='span_open'>" + "<i class='down_icon'></i>" + model.Text + "</span>");
                    sbHtml.Append("<ul class='open'>");
                    foreach (Vincent.Permission.Model.account_nodeslist node in lst)
                    {
                        sbHtml.Append("<li>");
                        //sbHtml.Append(string.Format("<a href='{0}' target='{1}'>{2}</a>", Common.VirtualPath()+ node.Url.Replace("\\","/"), node.Target, node.Text));
                        sbHtml.Append(string.Format("<a href='{0}' target='{1}'>{2}</a>", node.Url.Replace("\\", "/"), node.Target, node.Text));
                        sbHtml.Append("</li>");
                    }
                    sbHtml.Append("</ul>");

                    divLeftMenu.InnerHtml = sbHtml.ToString();
                }
            }
        }

        protected void TopMenu_Click(object sender, EventArgs e)
        {
            LinkButton lb = (LinkButton)sender;
            string nodeID = lb.Attributes["NodeID"].ToString();

            Common.SelectedNodeID = nodeID;

            LoadSubMenu();
        }

        protected void lblLogout_Click(object sender, EventArgs e)
        {
            //Response.Redirect(Common.VirtualPath() + "/relogin.aspx");
            Response.Redirect("/relogin.aspx");
        }

        protected void lblExit_Click(object sender, EventArgs e)
        {
            //Response.Redirect(Common.VirtualPath() + "/logout.aspx");
            Response.Redirect("/logout.aspx");
        }
    }
}